package com.nami.ucenter.domain.service;

import cn.hutool.core.collection.CollUtil;
import com.nami.gate.external.basic.cache.user.DeviceInfo;
import com.nami.gate.external.basic.cache.user.UserDeviceRel;
import com.nami.ucenter.domain.convertor.UserConvert;
import com.nami.ucenter.domain.model.UserModel;
import com.nami.ucenter.domain.port.repository.CacheUserRepository;
import com.nami.ucenter.domain.port.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserDomainService {

    private final CacheUserRepository cacheUserDomainService;
    private final UserRepository userRepository;


    public UserDeviceRel getUserInfoFromCacheOrDB(String appKey, String userId) {
        UserDeviceRel onrUser = cacheUserDomainService.getOnlUser(appKey, userId);
        if (onrUser == null) {
            UserModel user = userRepository.find(appKey, userId);
            return UserConvert.INSTANCE.convert(user);
        }
        return onrUser;
    }


    public List<DeviceInfo> getOnlDevices(String appKey, String userId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(userId)) {
            return null;
        }
        return cacheUserDomainService.getOnlDevicesByUserId(appKey, userId, null);
    }


    public List<UserDeviceRel> listUsersWithAttachedDevicesWhenOnline(String appKey, List<String> userIds) {
        if (StringUtils.isBlank(appKey) || CollUtil.isEmpty(userIds)) {
            return null;
        }
        log.info("[user-list] 获取当前用户信息，若在线则获取设备信息 appKey={}，size={}", appKey, userIds.size());
        List<UserDeviceRel> onlUsers = cacheUserDomainService.getOnlUsers(appKey, userIds);
        List<String> qryDbByUserIds = new ArrayList<>(userIds);
        if (CollUtil.isNotEmpty(onlUsers)) {
            List<String> currUserIds = onlUsers.stream()
                .map(UserDeviceRel::getUserId).collect(Collectors.toList());
            qryDbByUserIds.removeAll(currUserIds);
        }

        if (CollUtil.isNotEmpty(qryDbByUserIds)) {
            Map<String, UserModel> users = userRepository.getUsers(qryDbByUserIds);
            if (CollUtil.isNotEmpty(users)) {
                UserDeviceRel item = null;
                for (String userId : userIds) {
                    UserModel userModel = users.get(userId);
                    if (userModel != null) {
                        item = new UserDeviceRel();
                        item.setAppKey(userModel.getAppKey());
                        item.setUserId(userId);
                        item.setNick(userModel.getNickname());
                        item.setAvatar(userModel.getAvatarUrl());
                        onlUsers.add(item);
                    }
                }
            }
        }

        return onlUsers;
    }


}
